<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增任务' : '编辑任务'" append-to-body width="600px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="任务名称" prop="busMainData.jobName">
        <el-input v-model="form.busMainData.jobName" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="Bean名称" prop="busMainData.beanName">
        <el-input v-model="form.busMainData.beanName" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="参数内容">
        <el-input v-model="form.busMainData.methodParam" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="Cron表达式" prop="busMainData.cronExpression">
        <el-input v-model="form.busMainData.cronExpression" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="任务状态">
        <el-radio v-model="form.busMainData.pause" :label="false">启用</el-radio>
        <el-radio v-model="form.busMainData.pause" :label="true" >暂停</el-radio>
      </el-form-item>
      <el-form-item label="任务描述">
        <el-input v-model="form.busMainData.rmk" style="width: 460px;" rows="2" type="textarea"/>
      </el-form-item>
    </el-form>
    <div v-permission="['SYS_ADMIN']" slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrEdit, viewMainData, toAdd, toCopy } from '@/api/tools/job'
export default {
  props: {
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      isAdd: true,
      loading: false,
      dialog: false,
      style: 'width: 184px',
      form: {
        busMainData: {
          id: '',
          jobName: '',
          beanName: '',
          methodParam: '',
          pause: false,
          enableFlag: 'Y'
        }
      },
      rules: {
        'busMainData.jobName': [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ],
        'busMainData.beanName': [
          { required: true, message: '请输入bean名称', trigger: 'blur' }
        ],
        'busMainData.cronExpression': [
          { required: true, message: '请输入Cron表达式', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: { }}
      toAdd(data).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    copyData(mainId) {
      const data = { paramBean: { id: mainId }}
      toCopy(data).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    viewMainData(id) {
      const data = { paramBean: { id: id }}
      viewMainData(data).then(res => {
        this.form = res.data
      })
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>
